package com.yh.workbill.ui.count.mvp;

import android.view.View;

import com.yh.workbill.model.Count;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.ui.count.CountViewModel;

import java.util.List;
import java.util.TreeMap;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class Count_Presenter implements LifecycleObserver {
    private Count_View view;
    private Count_Model model;

    public Count_Presenter(Count_View view) {
        this.view = view;
        this.model = new Count_Model();
    }

    public void getCounts(CountViewModel viewModel, boolean isShowLoadding, boolean isRefresh, boolean isLoadMore, int pageindex, int pagenumber) {
        if (isShowLoadding) {
            view.showDialog("正在加载数据...", true);
        }
        model.getCountDatas(pageindex, pagenumber, new Count_Model.CountDataCallBack() {
            @Override
            public void onSuccess(List<Count> data) {
                if (isShowLoadding) {
                    view.hideDialog();
                }
                Count[] counts = data.toArray(new Count[data.size()]);
                if (isRefresh) {
                    view.refresh();
                    if (viewModel.getCountDatas().getValue() != null) {
                        viewModel.getCountDatas().getValue().clear();
                    }
                    viewModel.addCountDatas(0, counts);
                } else if (isLoadMore) {
                    int index = viewModel.getCountDatas().getValue().size();
                    viewModel.addCountDatas(index, counts);
                    view.loadMore();
                }
                view.getCounts(data);
            }

            @Override
            public void fail(String msg) {
                if (isShowLoadding) {
                    view.hideDialog();
                }
                view.fail(msg);
            }
        });
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestory() {
        if (this.view != null) {
            this.view = null;
        }
    }

}
